#include "sdf.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <gmssl/rand.h>

//********************************
//设备管理
//********************************

int SDF_OpenDevice(void ** phDeviceHandle) {
    return SDR_OK;
}

static int getRandom(char *r, int length) {
    if (r == NULL || length <= 0) {
        return -1;
    }
    if (RAND_bytes((unsigned char *)r, length) != 1) {
        return -1; // 生成随机数失败
    }
    return 0; // 生成随机数成功
}

int SDF_CloseDevice(void *hDeviceHandle) {
    return SDR_OK;
}

int SDF_GetDeviceInfo(void *hSessionHandle, DEVICEINFO *pstDeviceInfo) {
    DEVICEINFO di;
    strcpy(di.IssuerName, "RocSDF");
    strcpy(di.DeviceName, "SDFBESTI181x");
    strcpy(di.DeviceSerial, "2021040001");
    di.DeviceVersion = 1;
    //...

    *pstDeviceInfo = di;

    return SDR_OK;
}

int SDF_GenerateRandom(void *hSessionHandle, unsigned int uiLength, unsigned char *pucRandom) {
    if (pucRandom == NULL || uiLength == 0) {
        return -1; // 参数错误
    }
    if (getRandom((char *)pucRandom, uiLength) != 0) {
        return -1; // 随机数生成失败
    }
    return 0; // 成功
}
